/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.tarifador.view;

import br.com.tarifador.controller.RamalRN;
import br.com.tarifador.model.Ramal;
import br.com.tarifador.dataModel.RamalTableModel;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.hibernate.HibernateException;
import org.primefaces.context.RequestContext;

/**
 *
 * @author charles
 */
@ManagedBean
@SessionScoped
public class RamalBean {

    private RamalRN ramalRN = new RamalRN();
    private RamalTableModel ramalModel;
    private Ramal ramal = new Ramal();
    private Ramal ramalSelected;
    private List<Ramal> lista;

    public void salvarRamal() {
        boolean salvar = false;
        try {
            salvar = ramalRN.salvarRamal();

        } catch (HibernateException e) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao salvar o banco de dados " + e.getMessage(), ""));
        } catch (Throwable e) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao salvar os dados!" + e.getMessage(), ""));
        }
        if (salvar == false) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao salvar dados em brancos!", ""));
        } else {
            listaRamal();
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_INFO, "Operacao relizada com sucesso!!", ""));
        }
    }

    public void listaRamal() {
        lista = new ArrayList<Ramal>();
        try {
            lista = ramalRN.carregaListaRamal();

        } catch (HibernateException e) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao consultar o banco de dados", ""));
        } catch (Throwable e) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao salvar os dados!", ""));
        }
        this.ramalModel = new RamalTableModel(lista);
              
    }

    public void alteraRamal() {
        boolean valida = false;
        try {
            valida = ramalRN.updateRamal(ramalSelected);

        } catch (HibernateException e) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao consultar o banco de dados " + e.getMessage(), ""));
        } catch (Throwable e) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao salvar os dados! " + e.getMessage(), ""));
        }
        if (valida == false) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao salvar dados em brancos!", ""));
        } else {
            listaRamal();
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_INFO, "Operacao relizada com sucesso!!", ""));

        }
    }

    public void deleteRamal() {
        boolean valida = false;
        try {
            valida = ramalRN.deletaRamal(ramalSelected.getId());
        } catch (HibernateException e) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao salvar os dados!\n " + e.getMessage(), ""));
        } catch (Throwable e) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao salvar os dados!\n " + e.getMessage(), ""));
        }
        if (valida == false) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Falha ao salvar dados em brancos!", ""));
        } else {
            listaRamal();
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_INFO, "Operacao relizada com sucesso!!", ""));
        }
    }

    public void btnAlterarRamal() {
        RequestContext request = RequestContext.getCurrentInstance();
        try {
            if (ramalSelected.getId() != null && ramalSelected.getId() > 0) {
                request.addCallbackParam("sucesso", true);
            } else {
                request.addCallbackParam("sucesso", false);
            }
        } catch (NullPointerException e) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage("null", new FacesMessage(FacesMessage.SEVERITY_FATAL, "Selecione um campo!", ""));
        }
    }

    public void limpaLista() {
        if (ramalModel != null) {
            lista.clear();
            ramalModel = new RamalTableModel(lista);
        }
    }

    public RamalRN getRamalRN() {
        return ramalRN;
    }

    public void setRamalRN(RamalRN ramalRN) {
        this.ramalRN = ramalRN;
    }

    public RamalTableModel getRamalModel() {
        return ramalModel;
    }

    public void setRamalModel(RamalTableModel ramalModel) {
        this.ramalModel = ramalModel;
    }

    public Ramal getRamal() {
        return ramal;
    }

    public void setRamal(Ramal ramal) {
        this.ramal = ramal;
    }

    public Ramal getRamalSelected() {
        return ramalSelected;
    }

    public void setRamalSelected(Ramal ramalSelected) {
        this.ramalSelected = ramalSelected;
    }

    public List<Ramal> getLista() {
        return lista;
    }

    public void setLista(List<Ramal> lista) {
        this.lista = lista;
    }
    
}
